package com.api.gbhg.daiyanlu.statement;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.daiyanlu.Dao.TheReportCenterDao;
import com.api.gbhg.liuwenbo.utils.AlgorithmUtil;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import weaver.conn.RecordSet;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * 费用查询统计报表
 * Mr.dyl
 */
@Path("/query")
public class CostQueryStatistics {
    private PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
    TheReportCenterDao dao = new TheReportCenterDao();
    @POST
    @Path("/getQuery")
    @Produces(MediaType.APPLICATION_JSON)
    public String getQuery(Map<String,Object> maps) {
        Integer pageNum = 1;//当前页面
        Integer pageSize = 5;//显示数量
        String dw = "2";
        String bm = "";
        if(maps.containsKey("pageNum") && maps.get("pageNum") instanceof Integer &&(Integer)maps.get("pageNum")>0)
            pageNum = (Integer) maps.get("pageNum");
        if(maps.containsKey("pageSize") && maps.get("pageSize") instanceof Integer && (Integer)maps.get("pageSize")>0)
            pageSize = (Integer) maps.get("pageSize");
        if(maps.containsKey("bm") && maps.get("bm") instanceof Integer){
            bm = dao.getSectionId((Integer) maps.get("bm"));
        }
        if(maps.containsKey("dw") && maps.get("dw") instanceof Integer){
            dw =  AlgorithmUtil.getSubCompanIdBybmId((Integer) maps.get("dw"));
        }
        RecordSet rs = dao.getQuery(pageNum,pageSize,dw,bm);
        Map<String,Object>[] map = new HashMap[rs.getArray().size()];
        int i = 0;
        while (rs.next()){
            map[i] = new HashMap<String,Object>();
            map[i].put("bxlcmc",rs.getString("bxlcmc"));//流程名称
            map[i].put("lcbh",rs.getString("lcbh"));//流程编号
            RecordSet sbdwRs = rmcd.getSBDWById(rs.getInt("sbdw"));
            sbdwRs.next();
            map[i].put("ssdw",sbdwRs.getString("SUBCOMPANYNAME"));//预算单位
            RecordSet sbks = rmcd.getSBKSById(rs.getInt("sbks"));
            sbks.next();
            map[i].put("sbks",sbks.getString("departmentmark"));//预算部门
            map[i].put("bxr",rs.getString("bxr"));//创建人

            RecordSet sbdwRss = rmcd.getSBDWById(rs.getInt("supsubcomid"));
            sbdwRss.next();

            RecordSet sbkss = rmcd.getSBKSById(rs.getInt("bxrbm"));
            sbkss.next();

            map[i].put("dwbm",sbdwRss.getString("SUBCOMPANYNAME")+"-"+sbkss.getString("departmentmark"));//报销单位部门
            map[i].put("sqrq",rs.getString("sqrq"));//创建日期
            map[i].put("bxje",rs.getString("bxje"));//金额
            i++;
        }
        com.api.gbhg.zengping.page.PageInfo<String> pageInfo=new com.api.gbhg.zengping.page.PageInfo<>();
        int num = dao.getQueryCount(dw,bm);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(num);
        pageInfo.setList(map);
        return JSON.toJSONString(pageInfo);
    }
}
